<video
  id="video"
  phx-hook="video"
  poster={@media_background}
  controlsList="nodownload"
  controls
  autoplay
>
  <source src={@media_stream} type="video/mp4" />
</video>

<script>
  window['__onGCastApiAvailable'] = function(isAvailable) {

      if (isAvailable) {

        const context = cast.framework.CastContext.getInstance();

        context.setOptions({
          receiverApplicationId: 'B7C82081',
          autoJoinPolicy: chrome.cast.AutoJoinPolicy.ORIGIN_SCOPED
        });

        // Check if there is an existing session before starting a new one
        const session = context.getCurrentSession();

        if (session) {
          console.log('Using existing session:', session);
          loadMedia(session);

        } else {

          context.addEventListener(cast.framework.CastContextEventType.SESSION_STATE_CHANGED, function(event) {
            if (event.sessionState === cast.framework.SessionState.SESSION_STARTED) {
              console.log('New session started:', event.session);
              loadMedia(event.session);

            } else if (event.sessionState === cast.framework.SessionState.SESSION_ENDED ||
                       event.sessionState === cast.framework.SessionState.SESSION_ERROR) {
              console.log('Session ended:', event.session);
            }
          });

          context.requestSession().then(function(session) {
            console.log('New session requested:', session);
            loadMedia(session);
          }).catch(function(error) {
            console.log('Error requesting session:', error);
          });
        }

        function loadMedia(session) {
          const mediaInfo = new chrome.cast.media.MediaInfo('<%= System.get_env("APP_URL", "") <> assigns[:media_stream] %>', '<%= assigns[:mime_type] %>');

          mediaInfo.metadata = new chrome.cast.media.GenericMediaMetadata();
          mediaInfo.metadata.images = [new chrome.cast.Image('<%= System.get_env("APP_URL", "") <> assigns[:media_poster] %>')];
          mediaInfo.metadata.title = '<%= assigns[:page_title] %>';

          const request = new chrome.cast.media.LoadRequest(mediaInfo);

          session.loadMedia(request).then(function() {
            console.log('Media loaded');
          }).catch(function(error) {
            console.log('Error loading media:', error);
          });
        }
      }
  }
</script>
<script src="https://www.gstatic.com/cv/js/sender/v1/cast_sender.js?loadCastFramework=1">
</script>